package flc.ast.adapter;

import android.graphics.Color;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;
import java.util.Random;

import flc.ast.R;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class ChoiceAdapter extends StkProviderMultiAdapter<StkResBean> {

    public ChoiceAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new ChoiceProvider());
    }

    private class ChoiceProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_choice;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            Glide.with(baseViewHolder.itemView).load(bean.getThumbUrl()).into((ImageView) baseViewHolder.getView(R.id.ivChoiceItemImg));
            List<StkResBean> listSp = AppSpUtils.getLikeList();
            if (listSp != null && listSp.size() != 0) {
                for (StkResBean stkResBean : listSp) {
                    if (bean.equals(stkResBean)) {
                        bean.setSelected(true);
                        break;
                    }
                }
            }
            Random random = new Random();
            baseViewHolder.setText(R.id.tvChoiceItemNum, random.nextInt(20000) + "");
            if (bean.isSelected()) {
                baseViewHolder.setImageResource(R.id.ivChoiceItemLike, R.drawable.icon_dza);
                baseViewHolder.setTextColor(R.id.tvChoiceItemNum, Color.parseColor("#FF4A4A"));
            } else {
                baseViewHolder.setImageResource(R.id.ivChoiceItemLike, R.drawable.icon_dz);
                baseViewHolder.setTextColor(R.id.tvChoiceItemNum, Color.parseColor("#FFFFFF"));
            }
        }
    }

}
